home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / apps / database / ingres04.lzh / source / dbu / display.c < prev    next >
Encoding:
C/C++ Source or Header  |  1985-02-08  |  5.3 KB  |  296 lines

  1. # include    <ingres.h>
  2. # include    <aux.h>
  3. # include    <catalog.h>
  4. # include    <tree.h>
  5. # include    <symbol.h>
  6. # include    <access.h>
  7. # include     <func.h>
  8. # include    <pv.h>
  9. # include    <sccs.h>
  10. # include    <errors.h>
  11.  
  12. SCCSID(@(#)display.c    8.3    2/8/85)
  13.  
  14. /*
  15. **  DISPLAY -- display query corresponding to view, permission, 
  16. **        or intgerity declaration
  17. */
  18.  
  19.  
  20.  
  21.  
  22. extern short    tTdbu[];
  23. extern int    display();
  24. extern int    null_fn();
  25.  
  26. struct fn_def DsplayFn =
  27. {
  28.     "DISPLAY",
  29.     display,
  30.     null_fn,
  31.     null_fn,
  32.     NULL,
  33.     0,
  34.     tTdbu,
  35.     100,
  36.     'Z',
  37.     0
  38. };
  39. /*
  40. **  DISPLAY -- display query
  41. **
  42. **    Parameters:
  43. **        pc -- number of args in pv
  44. **        pv -- pv[i] == 4 for VIEW, 5 for PERMIT, 6 for INTEGRITY
  45. **                where i % 2 == 0
  46. **        pv[i] -- relation names for which pv[i-1] is mode
  47. **                where i%2==1
  48. **
  49. **    Returns:
  50. **        0 -- total success
  51. **        err -- error number of last error
  52. **
  53. **    Side Effects:
  54. **        prints out definition of appropriate characteristic of relation
  55. **
  56. **    Trace Flags:
  57. **        33, -1
  58. */
  59.  
  60.  
  61. display(pc, pv)
  62. int    pc;
  63. PARM    pv[];
  64. {
  65.     register int    ac;
  66.     register PARM    *av;
  67.     extern DESC    Treedes;
  68.     register int    i;
  69.     int        err;
  70.     char        err_array[PV_MAXPC];
  71.     auto int    mode;
  72.  
  73. #    ifdef xZTR1
  74.     if (tTf(50, -1))
  75.     {
  76.         printf("display: ");
  77.         prvect(pc, pv);
  78.     }
  79. #    endif
  80.  
  81.     err = 0;
  82.     if (pc % 2 != 0)
  83.         syserr("display: bad param count %d", pc);
  84.     opencatalog("tree", OR_READ);
  85.  
  86.     for (ac = 0, av = pv; ac < pc; av++, ac++)
  87.     {
  88.         mode = atoi(av->pv_val.pv_str);
  89.         av++;
  90.         err_array[ac++] = 0;
  91.         err_array[ac] = disp(av->pv_val.pv_str, mode);
  92.     }
  93.     for (ac = 0, av = pv; ac < pc; av++, ac++)
  94.     {
  95.         if (err_array[ac])
  96.             err = error(DISPERRBASE + err_array[ac], (av->pv_val).pv_str, 0);
  97.     }
  98.     return (err);
  99. }
  100. /* 
  101. ** DISP -- display integrity, permit, or define query on a relation
  102. **
  103. **    Finds a relation owned by the user or the DBA and passes
  104. **    the name and owner to the appropritae routine depending on
  105. **    mode.
  106. **
  107. **    Parameters:
  108. **        relation -- relation on which query is to be printed
  109. **        mode -- the print mode:
  110. **            4 -- view
  111. **            5 -- permit
  112. **            6 -- integrity
  113. **
  114. **    Returns:
  115. **        0 -- success
  116. **        1 -- no such relation, or none seeable by the user.
  117. **        3 -- VIEW mode and relation not a view
  118. **        4 -- PERMIT and no permissions on relation
  119. **        5 -- INTEGRITY mode and no integrity constraints
  120. **
  121. **    Trace Flags:
  122. **        33, 8
  123. */
  124.  
  125. disp(relation, mode)
  126. char    *relation;
  127. int    mode;
  128. {
  129.     DESC        d;
  130.     register int    i;
  131.     extern char    *Resrel;
  132.  
  133. #    ifdef xZTR1
  134.     if (tTf(50, 8))
  135.         printf("disp: relation %s\n", relation);
  136. #    endif
  137.  
  138.     Resrel = relation;
  139.     i = openr(&d, OR_RELTID, relation);
  140.     if (i > 0)
  141.         return (1);
  142.     else if (i < 0)
  143.         syserr("disp: openr(%s) ret %d", relation, i);
  144.     switch (mode)
  145.     {
  146.       case 4:        /* View query */
  147.         if (d.reldum.relstat & S_VIEW)
  148.             pr_def(relation, d.reldum.relowner);
  149.         else 
  150.             return (3);
  151.         break;
  152.  
  153.       case 5:
  154.         if (pr_prot(relation, &d))
  155.             return (4);
  156.         break;
  157.  
  158.       case 6:
  159.         if (d.reldum.relstat & S_INTEG)
  160.             pr_integrity(relation, d.reldum.relowner);
  161.         else
  162.             return (5);
  163.         break;
  164.  
  165.       default:
  166.         syserr("disp: mode == %d", mode);
  167.     }
  168.     return (0);
  169. }
  170. /*
  171. **  PR_DEF -- Print "define view" query of a view
  172. **
  173. **    Parameters:
  174. **        relation -- relation in question
  175. **        owner -- relowner
  176. **
  177. **    Returns:
  178. **        none
  179. **
  180. **    Side Effects:
  181. **        reads a tree, clears range table
  182. **
  183. **    Trace Flags:
  184. **        33, 9
  185. */
  186.  
  187. pr_def(relation, owner)
  188. char    *relation;
  189. char    *owner;
  190. {
  191.     register QTREE    *t;
  192.     QTREE        *gettree();
  193.  
  194. #    ifdef xZTR1
  195.     if (tTf(50, 9))
  196.         printf("pr_def(relation=\"%s\", owner=%s)\n", relation, owner);
  197. #    endif
  198.  
  199.     printf("View %s defined:\n\n", relation);
  200.     clrrange();
  201.  
  202.     /* Treeid == 0 because views have only one definition */
  203.     t = gettree(relation, owner, mdVIEW, 0,FALSE);
  204.     pr_range();
  205.     printf("define view ");
  206.     pr_tree(t);
  207. }
  208. /*
  209. **  PR_INTEGRITY -- print out integrity constraints on a relation
  210. **
  211. **    Finds all integrity tuples for this unique relation, and
  212. **    calls pr_int() to print a query from them.
  213. **
  214. **    Parameters:
  215. **        relid -- rel name
  216. **        relowner -- 2 byte owner id
  217. **
  218. **    Returns:
  219. **        none
  220. **
  221. **    Side Effects:
  222. **        file activity, query printing
  223. **
  224. **    Trace Flags:
  225. **        33, 9
  226. */
  227.  
  228. pr_integrity(relid, relowner)
  229. char    *relid;
  230. char    *relowner;
  231. {
  232.     extern DESC        Intdes;
  233.     TID            hitid, lotid;
  234.     struct integrity    key, tuple;
  235.     register int        i;
  236.  
  237.  
  238. #    ifdef xZTR1
  239.     if (tTf(50, 9))
  240.         printf("pr_integrity(relid =%s, relowner=%s)\n", 
  241.         relid, relowner);
  242. #    endif
  243.  
  244.     printf("Integrity constraints on %s are:\n\n", relid);
  245.     opencatalog("integrities", OR_READ);
  246.  
  247.     /* get integrities tuples for relid, relowner */
  248.     clearkeys(&Intdes);
  249.     setkey(&Intdes, &key, relid, INTRELID);
  250.     setkey(&Intdes, &key, relowner, INTRELOWNER);
  251.     if (i = find(&Intdes, EXACTKEY, &lotid, &hitid, &key))
  252.         syserr("pr_integrity: find %d", i);
  253.     for ( ; ; )
  254.     {
  255.         if (i = get(&Intdes, &lotid, &hitid, &tuple, TRUE))
  256.             break;
  257.         if (kcompare(&Intdes, &tuple, &key) == 0)
  258.             pr_int(&tuple, relid);
  259.     }
  260.     if (i != 1)
  261.         syserr("pr_integrity: get %d", i);
  262. }
  263. /*
  264. **  PR_INT -- print an integrity definition given a integrities tuple
  265. **
  266. **    Parameters:
  267. **        i -- integrity tuple
  268. **
  269. **    Returns:
  270. **        none
  271. **
  272. **    Side Effects:
  273. **        prints a query
  274. **        reads a tree
  275. */
  276.  
  277. pr_int(i, relid)
  278. register struct integrity    *i;
  279. char                *relid;
  280. {
  281.     register QTREE    *t;
  282.     QTREE        *gettree();
  283.  
  284.     clrrange();
  285.     t = gettree(i->intrelid, i->intrelowner, mdINTEG, i->inttree);
  286.     printf("Integrity constraint %d -\n\n", i->inttree);
  287.     pr_range();
  288.  
  289.     printf("define integrity on ");
  290.     pr_rv(Qt.qt_resvar = i->intresvar);
  291.     printf(" is "); 
  292.     pr_qual(t->right);
  293.     printf("\n\n\n");
  294.  
  295. }
  296.